// 根据时间戳格式化时间为**分钟前，**天前这种格式
function getFormattedTime1(timestamp) {
  const curTime = Date.parse(new Date()) / 1000;
  const delta = curTime - timestamp;
  const hour = 60 * 60;
  const day = 24 * hour;
  const month = 30 * day;
  const year = 12 * month;
  if (delta < hour) {
    // 显示多少分钟前
    const n = parseInt(delta / 60, 10);
    if (n === 0) {
      return "刚刚";
    }
    return `{${n}分钟前`;
  } else if (delta >= hour && delta < day) {
    return `${parseInt(delta / hour, 10)}小时前`;
  } else if (delta >= day && delta < month) {
    return `${parseInt(delta / day, 10)}天前`;
  } else if (delta >= month && delta < year) {
    return `${parseInt(delta / month, 10)}个月前`;
  }
}

function format(date, fmt) {
  let fmt1 = fmt;
  const o = {
    "M+": date.getMonth() + 1, // 月份
    "d+": date.getDate(), // 日
    "h+": date.getHours(), // 小时
    "m+": date.getMinutes(), // 分
    "s+": date.getSeconds(), // 秒
    "q+": Math.floor((date.getMonth() + 3) / 3), // 季度
    S: date.getMilliseconds(), // 毫秒
  };
  if (/(y+)/.test(fmt1)) {
    fmt1 = fmt1.replace(
      RegExp.$1,
      `${date.getFullYear()}`.substr(4 - RegExp.$1.length)
    );
  }
  for (const k in o) {
    if (new RegExp(`(${k})`).test(fmt1)) {
      fmt1 = fmt1.replace(
        RegExp.$1,
        RegExp.$1.length === 1 ? o[k] : `00${o[k]}`.substr(`${o[k]}`.length)
      );
    }
  }
  return fmt1;
}

function formatChatTime1(timestamp) {
  return format(new Date(timestamp * 1000), "hh:mm");
}

function formatChatDate1(timestamp) {
  // const date = format(new Date(timestamp * 1000), "MM月dd日 ");
  // console.log(date);
  return format(new Date(timestamp * 1000), "MM月dd日 ");
}

function currentTime1() {
  return Date.parse(new Date()) / 1000;
}

function formatMediaTime1(duration) {
  let min = Math.floor(duration / 60);
  let second = duration - min * 60;
  min = min >= 10 ? min : `0${min}`;
  second = second >= 10 ? second : `0${second}`;
  return `${min}":"${second}`;
}

function timeTohhmmss1(seconds) {
  let hh;
  let mm;
  let ss;

  if (seconds === null || seconds < 0) return;

  let pseconds = parseInt(seconds, 10);

  // 得到小时
  hh = (pseconds / 3600) | 0;

  pseconds = parseInt(pseconds, 10) - parseInt(hh, 10) * 3600;

  if (parseInt(hh, 10) < 10) {
    hh = `0${hh}`;
  }

  if (parseInt(hh, 10) >= 24) {
    hh = "00";
  }

  // 得到分钟
  mm = (parseInt(pseconds, 10) / 60) | 0;

  // 得到秒
  ss = parseInt(pseconds, 10) - parseInt(mm, 10) * 60;

  if (parseInt(mm, 10) < 10) {
    mm = `0${mm}`;
  }

  if (parseInt(ss, 10) < 10) {
    ss = `0${ss}`;
  }

  return `${hh}:${mm}:${ss}`;
}

function getTodayDate1() {
  const now = new Date();
  const yy = now.getFullYear();
  const mm = now.getMonth() + 1;
  const dd = now.getDate();
  const day = [];
  day[0] = "星期日";
  day[1] = "星期一";
  day[2] = "星期二";
  day[3] = "星期三";
  day[4] = "星期四";
  day[5] = "星期五";
  day[6] = "星期六";
  return `${yy}"年"${mm}月${dd}日${day[now.getDay()]}`;
}

module.exports = {
  getFormattedTime: getFormattedTime1,
  formatChatTime: formatChatTime1,
  formatChatDate: formatChatDate1,
  currentTime: currentTime1,
  formatMediaTime: formatMediaTime1,
  timeTohhmmss: timeTohhmmss1,
  getTodayDate: getTodayDate1,
  formatDate: format,
};
